package DBLayer;

import ModelLayer.*;

import java.sql.*;
import java.util.ArrayList;

public class DBTrainingCategory implements DBTrainingCategoryIF
{
	private  Connection con;
    
    public DBTrainingCategory() 
    {
      con = DBConnection.getInstance().getDBcon();
    }
	
    public ArrayList<TrainingCategory> getAllTrainingCategory(boolean retriveAssociation)
    {
        return miscWhere("", retriveAssociation);
    }

    public TrainingCategory searchTrainingCategoryID(int trainingCategoryID, boolean retriveAssociation)
    {   
    	String wClause = "TrainingCategoryID = " + trainingCategoryID + "";
        return singleWhere(wClause, retriveAssociation);
    }
    
    public TrainingCategory searchTrainingCategoryName(String name, boolean retriveAssociation)
    {
        String wClause = "Name  = " + name + "%'";
        System.out.println("Search TrainingCategory " + wClause);
        return singleWhere(wClause, retriveAssociation);
    }

    public int insertTrainingCategory(TrainingCategory tc)
    {
    	int nextId = GetMax.getMaxId("Select max(id) from TrainingCategory");
        nextId = nextId + 1;
        System.out.println("next id = " +  nextId);
    	
       int rc = -1;
	   String query="INSERT INTO TrainingCategory(TrainingCategoryID, Name, TrainingProgramID) VALUES (" + nextId + ",'" + tc.getName() + "','" + tc.getTrainingProgram().getTrainingProgramID() + "'";
                     
      System.out.println("insert : " + query);		  					
      try{
            Statement stmt = con.createStatement();
	 	  	stmt.setQueryTimeout(5);
	 	  	rc = stmt.executeUpdate(query);
	 	  	stmt.close();
		}	
	 	catch(Exception ex){
	 	  	System.out.println("Insert exception in TrainingCategory db: "+ex);
	   }
	   return(rc);
	}
	
	public int updateTrainingCategory(TrainingCategory tc)
	{
		TrainingCategory tcObj  = tc;
		int rc=-1;
	  
		String query="UPDATE TrainingCategory SET " +
			" WHERE TrainingCategoryID = '" + tcObj.getTrainingCategoryID() + "'" +
		 	"Name ='" + tcObj.getName() + "'" +
		 	"TrainingProgramID ='" + tcObj.getTrainingProgram().getTrainingProgramID() + "'";
			
            System.out.println("Update query:" + query);
  		try
  		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 	 	rc = stmt.executeUpdate(query);
	 	 	stmt.close();
		}
	 	catch(Exception ex)
	 	{
	 	 	System.out.println("Update exception in TrainingCategory db: "+ex);
	  	}
		return(rc);
	}
	
	public int delete(int id)
	{
               int rc=-1;
	  
	  	String query="DELETE FROM TrainingCategory WHERE id = " +
				id + "";
                System.out.println(query);
	  	try
	  	{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 	  	rc = stmt.executeUpdate(query);
	 	  	stmt.close();
  		}
   	    catch(Exception ex)
   	    {
	 	  	System.out.println("Delete exception in TrainingCategory db: "+ex);
   	    }
		return(rc);
	}
	
	private ArrayList<TrainingCategory> miscWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		ArrayList<TrainingCategory> trainingCategoryList = new ArrayList<TrainingCategory>();	
		
		String query =  buildQuery(wClause);
  
  		try
  		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
			while( results.next() )
			{
				TrainingCategory tcObj = new TrainingCategory();
				tcObj = buildTrainingCategory(results);	
				trainingCategoryList.add(tcObj);	
			}
			stmt.close();
		}
	 	catch(Exception e)
	 	{
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
		return trainingCategoryList;
	}
	
	private TrainingCategory singleWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		TrainingCategory tcObj = new TrainingCategory();
	    String query =  buildQuery(wClause);
//        System.out.println(query);
		try
		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() )
	 		{
	 			tcObj = buildTrainingCategory(results);
	 			stmt.close();
			}
		}	
	 	catch(Exception e)
	 	{
	 		System.out.println("Query exception: "+e);
	 	}
		return tcObj;
	}

	private String buildQuery(String wClause)
	{
	    String query="SELECT * FROM TrainingCategory tC, TrainingProgram tP";
	    query=query+" WHERE tC.TrainingProgramID = tP.TrainingProgramID";
		if (wClause.length()>0)
			query=query+" AND " + wClause;
		return query;
	}

	private TrainingCategory buildTrainingCategory(ResultSet results)
      {   
		TrainingCategory tcObj = new TrainingCategory();
          try
          {
        	  	tcObj.setTrainingCategoryID(results.getInt(1));
                tcObj.setName(results.getString(2));
                tcObj.setTrainingProgram(new TrainingProgram(
                								results.getInt(4),
                								results.getString(5),
                								results.getString(6),
                								results.getString(7)
                						));
          }
         catch(Exception e)
         {
             System.out.println("error in building the TrainingCategory object");
         }
         return tcObj;
      }

}
